package com.ysx.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysx.article.feign.ApAuthorFeign;
import com.ysx.article.pojo.ApAuthor;
import com.ysx.common.constants.BusinessConstants;
import com.ysx.exception.ToutiaoException;
import com.ysx.user.mapper.ApUserMapper;
import com.ysx.user.mapper.ApUserRealnameMapper;
import com.ysx.user.pojo.ApUser;
import com.ysx.user.pojo.ApUserRealname;
import com.ysx.user.service.ApUserRealnameService;
import com.ysx.wemedia.feign.WeMediaFeign;
import com.ysx.wemedia.pojo.WmUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2021-09-08
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    @Autowired
    private WeMediaFeign weMediaFeign;

     //审核通过
    @Override
    public void auditPass(Long id) throws ToutiaoException {
        //根据id查询数据库是否存在记录
        ApUserRealname apUserRealNameDB = apUserRealnameMapper.selectById(id);
        if (ObjectUtils.isEmpty(apUserRealNameDB)) {
            throw new ToutiaoException("数据库中查询不到记录");
        }
        //1,修改身份认证表状态信息
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id.intValue());
        apUserRealname.setStatus(BusinessConstants.AdUserRealNameCT.AUDIO_PASS);
        apUserRealnameMapper.updateById(apUserRealname);

        //根据用户id查询用户信息
        Integer userId = apUserRealNameDB.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        //2,创建自媒体账号 TODO
        WmUser vmUser_fromFeign = weMediaFeign.selectByUserId(userId);
        WmUser wmUser = new WmUser();
        if (ObjectUtils.isEmpty(vmUser_fromFeign)) {
            BeanUtils.copyProperties(apUser,wmUser);
            //单独设置用户id
            wmUser.setApUserId(userId);
            wmUser = weMediaFeign.save(wmUser).getData();
        }
        ApAuthor apAuthorFromFeign = apAuthorFeign.selectByUserId(userId);
        //3,创建文章账号  TODO
        if (ObjectUtils.isEmpty(apAuthorFromFeign)) {
            ApAuthor apAuthor = new ApAuthor();
            BeanUtils.copyProperties(wmUser,apUser);
            apAuthor.setWmUserId(wmUser.getId());//媒体人id
            apAuthor.setUserId(userId);//用户id
            apAuthor.setName(wmUser.getName());
            apAuthorFeign.save(apAuthor);
        }
    }
     //审核失败
    @Override
    public void auditFailure(Long id,String reason) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id.intValue());
        apUserRealname.setStatus(BusinessConstants.AdUserRealNameCT.AUDIT_FAILED);
        apUserRealname.setReason(reason);
        apUserRealnameMapper.updateById(apUserRealname);
    }
}
